package com.jddup.program.servlet;

import com.alibaba.druid.util.Utils;
import com.alibaba.fastjson.JSON;
import com.jddup.common.pojo.vo.BaseResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by ZZB on 2017/12/18.
 */
public class JddupServlet extends HttpServlet {
    protected final String resourcePath = "html";
    private JddupService jddupService;

    public JddupServlet() {
        jddupService = new JddupService();
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        String requestURI = request.getRequestURI();

        response.setCharacterEncoding("utf-8");
        if (contextPath == null) { // root context
            contextPath = "";
        }
        String uri = contextPath + servletPath;
        String path = requestURI.substring(contextPath.length() + servletPath.length());
        if (path.contains(".json")) {
            String fullUrl = path;
            if (request.getQueryString() != null && request.getQueryString().length() > 0) {
                fullUrl += "?" + request.getQueryString();
            }
            response.getWriter().print(process(fullUrl, request));
            return;
        } else if (path.equals("")) {
            path = "index.html";
        }

        // find file in resources path

        returnResourceFile(path, uri, response);

    }


    protected void returnResourceFile(String fileName, String uri, HttpServletResponse response)
            throws ServletException,
            IOException {

        String filePath = getFilePath(fileName);

        if (filePath.endsWith(".html")) {
            response.setContentType("text/html; charset=utf-8");
        }
        if (fileName.endsWith(".jpg") || fileName.endsWith(".png") || fileName.endsWith(".gif")) {
            byte[] bytes = Utils.readByteArrayFromResource(filePath);
            if (bytes != null) {
                response.getOutputStream().write(bytes);
            }

            return;
        }

        String text = Utils.readFromResource(filePath);
        if (text == null) {
            response.sendRedirect(uri + "/index.html");
            return;
        }
        if (fileName.endsWith(".css")) {
            response.setContentType("text/css;charset=utf-8");
        } else if (fileName.endsWith(".js")) {
            response.setContentType("text/javascript;charset=utf-8");
        }
        response.getWriter().write(text);
    }

    protected String getFilePath(String fileName) {
        return resourcePath + fileName;
    }

    protected String process(String url, HttpServletRequest request) {
        BaseResponse resp = null;
        if (url.equals("/index.json")) {
            resp = jddupService.datasourceIndex("someKey");
        } else if (url.contains("/column.json")) {
            resp = jddupService.columnDiffVO(request.getParameter("tableName"), request.getParameter("columnName"));
        } else if (url.contains("/tableColumn.json")) {
            resp = jddupService.tableColumnDiffVO(request.getParameter("tableName"));
        } else if (url.contains("/executeSQL.json")) {
            resp = jddupService.executeSQL(request.getParameter("tableName"), request.getParameter("columnName"),
                    request.getParameter("sql"));
        } else if (url.contains("/executeCreateSQL.json")) {
            resp = jddupService.executeCreateSQL(request.getParameter("tableName"));

        }else {
            resp = jddupService.datasourceIndex("someKey");
        }
        return JSON.toJSONString(resp);
    }
}
